package com.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class IdFlag implements WritableComparable<IdFlag> {
    private String id;
    // 设置标记，第一个文件标记为0，第二个文件标记为1
    private int flag;

    public IdFlag() {
    }

    public IdFlag(String id, int flag) {
        this.id = id;
        this.flag = flag;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return "IdFlag{" +
                "id='" + id + '\'' +
                ", flag=" + flag +
                '}';
    }

    @Override
    public int compareTo(IdFlag o) {
        // 先按编号排序，编号相同的情况下，根据标记进行排序，
        // 以此识别哪个文件的数据在前
        int n = this.id.compareTo(o.getId());
        if (n==0){
            return this.getFlag()-o.getFlag();
        }
        return n;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(id);
        out.writeInt(flag);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readUTF();
        this.flag = in.readInt();
    }
}
